package com.health.controller.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.constant.MessageConstant;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.User;
import com.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Classname UserController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/17 8:52
 * @Author RainGrd
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Reference
    private UserService userService;

    //获取当前登录用户的用户名
    @RequestMapping("/getUsername.do")
    public Result getUsername() {
        try {
            org.springframework.security.core.userdetails.User user =
                    (org.springframework.security.core.userdetails.User)
                            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return Result.success(user.getUsername(), MessageConstant.GET_USERNAME_SUCCESS);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 分页查询预约管理
     *
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('USER_QUERY')")//权限校验
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("queryPageBean:{}", queryPageBean);
        return userService.queryUserPage(queryPageBean);
    }

    @PreAuthorize("hasAuthority('USER_ADD')")
    @RequestMapping("/add.do")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        log.info("要添加的用户对象:{}", user);
        log.info("要添加角色权限ID:{}", Arrays.asList(roleIds));
        return userService.saveUser(user, roleIds);
    }

    @PreAuthorize("hasAuthority('USER_EDIT')")
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        log.info("要添加的用户对象:{}", user);
        log.info("要修改的用户对象:{}", Arrays.asList(roleIds));
        return userService.editUserByRoles(user, roleIds);
    }

    @RequestMapping("/findRoleIdsByMenuId.do")
    public Result findRoleIdsByMenuId(@Param("userId") Integer userId) {
        log.info("userId:{}", userId);
        List<Integer> userIds = userService.queryRoleIdsByUserId(userId);
        log.info("menuIds:{}", userIds);
        if (userIds != null && userIds.size() > 0) {
            return Result.success(userIds);
        }
        return Result.error();
    }

    /**
     * 查询所有启用的健康管理师
     *
     * @return
     */
    @RequestMapping("/findUserRoleAll.do")
    public Result findUserRoleAll() {
        return userService.queryUserRoleAll();
    }

    @PreAuthorize("hasAuthority('USER_DELETE')")
    @RequestMapping("/delete.do")
    public Result delete(@RequestParam("id") Integer id) {
        log.info("要删除的预约管理id:{}", id);
        return userService.clearUser(id);
    }


    @RequestMapping("/getUserByUserName.do")
    public Result getUserByUserName(@Param("userName") String userName) {
        User user = userService.queryByUserName(userName);
        //进行解码
        return Result.success(user);
    }

    @RequestMapping("/updatePassword.do")
    public Result updatePassword(@RequestBody User user) {
        log.info("要修改密码的用户:{}", user);
        return userService.editUserPassword(user);
    }

    @RequestMapping("/updateUserFlag.do")
    public Result updateUserFlag(@RequestBody User user) {
        return userService.editUserFlag(user);
    }


}
